package c.c.c.a.a;

import android.os.Build.VERSION;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
import java.lang.reflect.Field;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: LooperMonitor */
public class h {
    public static int a;
    public static List<a> b;
    public static volatile AtomicLong c = new AtomicLong(-1);
    public static long d = 100;
    public static long e;
    public static HandlerThread f;
    public static long g;
    public static long h;
    public static Handler i;
    public static int j;
    public static boolean k;
    public static volatile String l = null;
    public static volatile boolean m = false;
    public static int n = -1;
    public static long o = -1;
    public static long p = -1;
    public static int q = -1;
    public static MessageQueue r = null;
    public static Field s = null;
    public static Field t = null;

    /* compiled from: LooperMonitor */
    public static class a {
        public int a;
        public int b;
        public long c;
        public long d;
        public long e;
        public String f;

        public String toString() {
            int i = this.b;
            String str = ",cpuTime:";
            String str2 = " tick , mDuration：";
            StringBuilder g;
            if (i == 0) {
                g = c.b.a.a.a.g("[[[ IDLE  ]]] cost ");
                g.append(this.c);
                g.append(str2);
                g.append(this.d);
                g.append(str);
                g.append(this.e);
                return g.toString();
            } else if (i == 1) {
                g = c.b.a.a.a.g("[[[ Long IDLE  ]]] cost ");
                g.append(this.c);
                g.append(str2);
                g.append(this.d);
                g.append(str);
                g.append(this.e);
                return g.toString();
            } else if (i == 2) {
                g = c.b.a.a.a.g("[[[  1 msg  ]]] cost ");
                g.append(this.c);
                g.append(str2);
                g.append(this.d);
                g.append(str);
                g.append(this.e);
                g.append(", msg:");
                g.append(this.f);
                return g.toString();
            } else if (i == 3) {
                g = c.b.a.a.a.g("[[[ 1 msg + IDLE  ]]] cost ");
                g.append(this.c);
                g.append(str2);
                g.append(this.d);
                g.append(str);
                g.append(this.e);
                return g.toString();
            } else {
                str2 = " msg:";
                String str3 = "cpuTime:";
                String str4 = "[[[ ";
                if (i == 4) {
                    g = c.b.a.a.a.g(str4);
                    g.append(this.a - 1);
                    g.append(" msgs  ]]] cost less than 1 tick, [[[  last msg ]]] cost more than ");
                    g.append(this.c - 1);
                    g.append("tick ,, mDuration：");
                    g.append(this.d);
                    g.append(str3);
                    g.append(this.e);
                    g.append(str2);
                    g.append(this.f);
                    return g.toString();
                } else if (i == 5) {
                    g = c.b.a.a.a.g(str4);
                    g.append(this.a);
                    g.append(" msgs ]]] cost less than 1 tick but [[[  IDLE ]]] cost more than");
                    g.append(this.c - 1);
                    g.append(" ticks, , mDuration：");
                    g.append(this.d);
                    g.append(str3);
                    g.append(this.e);
                    return g.toString();
                } else if (i == 6) {
                    g = c.b.a.a.a.g("[[[  1 msg  ]]] cost less than 1 tick , but [[[  IDLE ]]] cost more than");
                    g.append(this.c - 1);
                    g.append(", , mDuration：");
                    g.append(this.d);
                    g.append(str3);
                    g.append(this.e);
                    return g.toString();
                } else {
                    String str5 = " cost cpuTime:";
                    if (i == 7) {
                        g = c.b.a.a.a.g(str4);
                        g.append(this.a);
                        g.append(" msgs + IDLE  ]]] cost 1 tick , mDuration：");
                        g.append(this.d);
                        g.append(str5);
                        g.append(this.e);
                        return g.toString();
                    } else if (i == 8) {
                        g = c.b.a.a.a.g("[[[ 1 msgs ]]] cost ");
                        g.append(this.c);
                        g.append(" ticks , mDuration：");
                        g.append(this.d);
                        g.append(str5);
                        g.append(this.e);
                        g.append(str2);
                        g.append(this.f);
                        return g.toString();
                    } else if (i == 9) {
                        g = c.b.a.a.a.g(str4);
                        g.append(this.a);
                        g.append(" msgs ]]] cost 1 tick , mDuration：");
                        g.append(this.d);
                        g.append(str5);
                        g.append(this.e);
                        return g.toString();
                    } else {
                        g = c.b.a.a.a.g("=========   UNKNOW =========  Type:");
                        g.append(this.b);
                        g.append(" cost ticks ");
                        g.append(this.c);
                        g.append(" msgs:");
                        g.append(this.a);
                        return g.toString();
                    }
                }
            }
        }
    }

    public static Message a(MessageQueue messageQueue) {
        Field field = s;
        if (field == null) {
            try {
                field = Class.forName("android.os.MessageQueue").getDeclaredField("mMessages");
                s = field;
                field.setAccessible(true);
                return (Message) s.get(messageQueue);
            } catch (Exception unused) {
                return null;
            }
        }
        try {
            return (Message) field.get(messageQueue);
        } catch (Exception unused2) {
            return null;
        }
    }

    public static JSONObject b(Message message, long j) {
        JSONObject jSONObject = new JSONObject();
        if (message == null) {
            return jSONObject;
        }
        try {
            jSONObject.put("when", message.getWhen() - j);
            if (message.getCallback() != null) {
                jSONObject.put("callback", message.getCallback());
            }
            jSONObject.put("what", message.what);
            if (message.getTarget() != null) {
                jSONObject.put("target", message.getTarget());
            } else {
                jSONObject.put("barrier", message.arg1);
            }
            jSONObject.put("arg1", message.arg1);
            jSONObject.put("arg2", message.arg2);
            Object obj = message.obj;
            if (obj != null) {
                jSONObject.put("obj", obj);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jSONObject;
    }

    public static MessageQueue c() {
        if (r == null && Looper.getMainLooper() != null) {
            Looper mainLooper = Looper.getMainLooper();
            if (mainLooper == Looper.myLooper()) {
                r = Looper.myQueue();
            } else if (VERSION.SDK_INT >= 23) {
                r = mainLooper.getQueue();
            } else {
                try {
                    Field declaredField = mainLooper.getClass().getDeclaredField("mQueue");
                    declaredField.setAccessible(true);
                    r = (MessageQueue) declaredField.get(mainLooper);
                } catch (Throwable th) {
                    th.printStackTrace();
                }
            }
        }
        return r;
    }

    public static a d() {
        int size = b.size();
        int i = a;
        if (size == i) {
            size = (q + 1) % i;
            q = size;
            return (a) b.get(size);
        }
        a aVar = new a();
        b.add(aVar);
        q++;
        return aVar;
    }

    /* JADX WARNING: Removed duplicated region for block: B:22:0x005b  */
    /* JADX WARNING: Removed duplicated region for block: B:21:0x0058  */
    /* JADX WARNING: Removed duplicated region for block: B:18:0x0053 A:{SYNTHETIC, Splitter:B:18:0x0053} */
    /* JADX WARNING: Removed duplicated region for block: B:21:0x0058  */
    /* JADX WARNING: Removed duplicated region for block: B:22:0x005b  */
    /* JADX WARNING: Removed duplicated region for block: B:26:0x007e A:{SYNTHETIC, Splitter:B:26:0x007e} */
    public static long e() {
        /*
        r0 = n;
        r1 = new java.lang.StringBuilder;
        r1.<init>();
        r2 = "/proc/self/task/";
        r1.append(r2);
        r1.append(r0);
        r0 = "/stat";
        r1.append(r0);
        r0 = r1.toString();
        r1 = 0;
        r2 = new java.io.BufferedReader;	 Catch:{ Exception -> 0x004c, all -> 0x004a }
        r3 = new java.io.InputStreamReader;	 Catch:{ Exception -> 0x004c, all -> 0x004a }
        r4 = new java.io.FileInputStream;	 Catch:{ Exception -> 0x004c, all -> 0x004a }
        r4.<init>(r0);	 Catch:{ Exception -> 0x004c, all -> 0x004a }
        r3.<init>(r4);	 Catch:{ Exception -> 0x004c, all -> 0x004a }
        r0 = 1000; // 0x3e8 float:1.401E-42 double:4.94E-321;
        r2.<init>(r3, r0);	 Catch:{ Exception -> 0x004c, all -> 0x004a }
        r0 = r2.readLine();	 Catch:{ Exception -> 0x0048 }
        r3 = ")";
        r3 = r0.indexOf(r3);	 Catch:{ Exception -> 0x0048 }
        if (r3 <= 0) goto L_0x003c;
    L_0x0036:
        r3 = r3 + 2;
        r0 = r0.substring(r3);	 Catch:{ Exception -> 0x0048 }
    L_0x003c:
        r3 = " ";
        r1 = r0.split(r3);	 Catch:{ Exception -> 0x0048 }
        r2.close();	 Catch:{ all -> 0x0046 }
        goto L_0x0056;
        goto L_0x0056;
    L_0x0048:
        r0 = move-exception;
        goto L_0x004e;
    L_0x004a:
        r0 = move-exception;
        goto L_0x007c;
    L_0x004c:
        r0 = move-exception;
        r2 = r1;
    L_0x004e:
        r0.printStackTrace();	 Catch:{ all -> 0x007a }
        if (r2 == 0) goto L_0x0056;
    L_0x0053:
        r2.close();	 Catch:{ all -> 0x0046 }
    L_0x0056:
        if (r1 != 0) goto L_0x005b;
    L_0x0058:
        r0 = -1;
        goto L_0x0079;
    L_0x005b:
        r0 = 11;
        r0 = r1[r0];
        r2 = java.lang.Long.parseLong(r0);
        r4 = c.c.c.a.i.k.a.a();
        r4 = r4 * r2;
        r0 = 12;
        r0 = r1[r0];
        r0 = java.lang.Long.parseLong(r0);
        r2 = c.c.c.a.i.k.a.a();
        r2 = r2 * r0;
        r0 = r2 + r4;
    L_0x0079:
        return r0;
    L_0x007a:
        r0 = move-exception;
        r1 = r2;
    L_0x007c:
        if (r1 == 0) goto L_0x0081;
    L_0x007e:
        r1.close();	 Catch:{ all -> 0x0081 }
    L_0x0081:
        throw r0;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.c.a.a.h.e():long");
    }
}
